﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class Beatline : MonoBehaviour
    {
        public GameObject lineShow;
        public int index;
        JudgementLine jl;
        public TextMesh showIndex;
        public bool below;
        public int onDensityIn;
        void Start()
        {

        }
        public void Init(JudgementLine jl)
        {
            this.jl = jl;
            showIndex.text = string.Format("{1}_{0}", (index * LevelController.density), index);
            
        }


        void Update()
        {
            UpdatePos();
            if (onDensityIn == 0)
                showIndex.text = string.Format("{1}_{0}", (index * LevelController.density), index);
        }

        public void ChangeBeatlineDensity()
        {
            int d = LevelController.instance.nowDensity;
            bool bool1 = false;
            switch (d)
            {
                case 1: bool1 = true; break;
                case 2: bool1 = onDensityIn % 2 == 0; break;
                case 4: bool1 = onDensityIn % 4 == 0; break;
                case 8: bool1 = onDensityIn % 8 == 0; break;
                case 16: bool1 = onDensityIn == 16; break;
                case 32: bool1 = false; break;
            }

            if (!bool1) bool1 = onDensityIn == 0;
            if (LevelController.instance.showBeatline && ((transform.localPosition.y > 0 && !below) || (transform.localPosition.y < 0 && below)) && bool1)
            {
                if (!lineShow.active) lineShow.active = true;
            }
            else
            {
                if (lineShow.active) lineShow.active = false;
            }
        }

        public void UpdatePos()
        {
            float p = (float)jl.GetSpeedDistance(Utils.GetTimeFromBeatTime(index * 32 + onDensityIn, jl.bpm));
            if (below) p = -p;
            transform.localPosition = new Vector3(0, p, 0);
        }
    }
}
